package veterinaria.servlet;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.IOException;
//import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import trastienda.modelo.Rol;
import veterinaria.excepcion.DAOExcepcion;
import veterinaria.modelo.Aviso;
//import veterinaria.modelo.Doctor;
import veterinaria.negocio.GestionAviso;
//import veterinaria.negocio.GestionDoctores;

/**
 * Servlet implementation class avisoInsertar
 */
@WebServlet("/AviInsertar")
public class AviInsertar extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AviInsertar() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String titulo = request.getParameter("txttitulo");
		String descripcion = request.getParameter("txtdescripcion");
		String fecha = request.getParameter("txtfecha");
		//Date calendar = new GregorianCalendar;
		//new SimpleDateFormat("yyyy/MM/dd");
		
		DateFormat sdff = new SimpleDateFormat("yyyy-MM-dd");
		Date frmDate;
		try {
			frmDate = sdff.parse(fecha);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
	
		GestionAviso negocio = new GestionAviso();
		
		try {
			Aviso aviso = negocio.insertar(titulo, descripcion, fecha);
			if (aviso==null){
				response.sendRedirect(request.getContextPath()	+ "/avisoError.jsp");
			
						
			}
			
			else{
				
	
				
							
				Collection<Aviso> aviso1 = negocio.listar();
				request.setAttribute("AVISO", aviso1);
								RequestDispatcher rd = request.getRequestDispatcher("aviso.jsp");
				rd.forward(request, response);

				
				
				//response.sendRedirect(request.getContextPath()	+ "/aviso.jsp");
			}
		
		
		} catch (DAOExcepcion e) {
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
		
	
	}
}
